import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:flutterapp1/ATPlayWidget.dart';
import 'color/MyNetTool.dart';
import 'Base/BaseWidget/BaseWidget.dart';
import 'color/MyColor.dart';
import 'color/MyEncrypt.dart';
import 'model/BaseModel.dart';
import 'dart:convert' as convert;
import 'model/ATVideoInfo.dart';
import 'color/MyNetTool.dart';
import 'view/AppNavi.dart';
import 'view/ATClassifyItem.dart';
import 'model/ATVideo.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
class Video extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
      ),
      home:ATVideoStatefulWidget(title: "视频",),
    );
  }
}
class ATVideoStatefulWidget extends BaseStatefulWidget{
  ATVideoStatefulWidget({Key key, this.title}) : super(key: key);

  final String title;
  @override
  State<StatefulWidget> createState() {
    return _VideoCtrlState();
  }
}

class _VideoCtrlState extends State<ATVideoStatefulWidget> {
  var listData = [];
  ScrollController _scrollController = ScrollController();
  void initState(){
    super.initState();
    loadData();

    _scrollController.addListener(
          () {
        if (_scrollController.position.pixels ==
            _scrollController.position.maxScrollExtent) {

        }
      },
    );
  }
  Future <void> onRefresh() async {
    await Future.delayed(Duration(seconds: 3), (){
      loadData();
    });
  }
  void loadData() async{
    ATHomeVideo home =  await ATNetVideoTool.home("15");
    listData.clear();
    home.data.forEach((ATVideoCate cate){
      listData.add(cate);
      cate.vod.forEach((info){
        listData.add(info);
      });
    });
    setState(() {
    });
  }
  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    //ScreenUtil.init(context, width: 750, height: 1334);
    return Scaffold(
      appBar: AppNavi(
        title:widget.title,
        backgroundColor: MyColor.AppColor,
        textColor: MyColor.AppFFFFFF,
        showBack: false,
      ),

      body: Center(

        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Container(
              height: (AppHeigh - AppStatusBar - AppNaviBar - AppToolBar),
              width:  AppWidth,
              color: MyColor.AppFFFFFF,
              child: new RefreshIndicator(child: new StaggeredGridView.countBuilder(
                controller: _scrollController,
                crossAxisCount: 3,
                mainAxisSpacing: 10.0,
                crossAxisSpacing: 15.0,
                padding: EdgeInsets.all(15.0),
                itemCount: listData.length,
                staggeredTileBuilder: (int index) {
                  String name = listData[index].runtimeType.toString();
                  if(name == "ATVideoCate"){
                    return new StaggeredTile.count(3,0.3);
                  } else {
                    return new StaggeredTile.count(1,2);
                  }
                },
                itemBuilder: (context, index) {
                  String name = listData[index].runtimeType.toString();
                  if(name == "ATVideoCate"){
                    ATVideoCate cate = listData[index];
                    return ATClassifyHeader(title: cate.name,subTitle: "更多",onTap:(){

                    },);
                  }else{
                    ATVideo info = listData[index];
                    return ATClassifyItem(title: info.name,subTitle: info.type,url: info.pic,onTap: (){

                      Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (context) => new ATPlayVideoStatefulWidget(title:"关于",)));
                    });
                  }
                },
              ), onRefresh: onRefresh),

            ),
          ],
        ),
      ),
    );
  }
}